package com.tools.cleanmaster.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tools.ad.util.GARecordUtils;
import com.tools.ad.util.StatusBarUtil;
import com.tools.cleanmaster.CleanGAConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.bean.AutoCleanFeature;
import com.tools.cleanmaster.manager.AutoCleanManager;
import com.tools.cleanmaster.ui.base.SingleActivity;
import com.tools.cleanmaster.ui.dialog.FrequencyChooseDialog;
import com.tools.cleanmaster.ui.view.ToastHelper;
import com.tools.cleanmaster.ui.view.header.HeadHelper;
import com.tools.cleanmaster.utils.StorageUtils;

/**
 * Created by wuyanfei on 16/4/25.
 */
public class AutoCleanSettingActivity extends SingleActivity implements View.OnClickListener {

    public static final String FROM_EXTRA = "from";

    private ImageButton mEnableSwitch;
    private LinearLayout mEnableLayout, mFrequencyLayout;
    private TextView mFrequencyTitle, mFrequencySummary;
    private TextView mTotalCleanSize, mTotalCleanSummary;
    private RelativeLayout mTotalCleanLayout;
    private FrequencyChooseDialog mChooseFrequencyDialog;

    private ImageButton mAutoCleanRAMButton;
    private ImageButton mLowMemoryButton;
    private TextView mLowMemorySummary;
    private ViewGroup mRamEnableLayout;
    private ViewGroup mLowMemoryLayout;

    private String mFrom;
    private boolean mIsEnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_auto_clean);

        StatusBarUtil.setTransparent(this);
        HeadHelper.setupMainHeader(this, R.id.titlebar, R.string.item_card_smart_clean_title, this).hideSettingButton();

        //初始化 缓存清理设置项
        mEnableLayout = findViewById(R.id.enable_layout);
        mEnableSwitch = findViewById(R.id.auto_clean_switch);
        mEnableLayout.setOnClickListener(this);
        mEnableSwitch.setOnClickListener(this);
        mFrequencyLayout = findViewById(R.id.auto_clean_freq_layout);
        mFrequencyLayout.setOnClickListener(this);
        mFrequencyTitle = findViewById(R.id.auto_clean_freq_title);
        mFrequencySummary = findViewById(R.id.auto_clean_freq_desc);
        mTotalCleanSize = findViewById(R.id.auto_clean_total_size);
        mTotalCleanSummary = findViewById(R.id.auto_clean_total_summary);

        long totalSize = imContext.getAutoCleanManager().getAutoCleanTotalSize();
        long storgeSize = StorageUtils.getStorageTotalSize(this);
        long sizeLimit = (long) (storgeSize * 0.6);
        if (totalSize >= sizeLimit) {
            mTotalCleanSize.setText("> " + Formatter.formatFileSize(this, sizeLimit) + " ");
        } else {
            mTotalCleanSize.setText(Formatter.formatFileSize(this, totalSize) + " ");
        }

        mTotalCleanLayout = findViewById(R.id.total_clean_layout);
        mTotalCleanLayout.setOnClickListener(this);

        mFrom = getIntent().getStringExtra(FROM_EXTRA);
        if (TextUtils.isEmpty(mFrom)) {
            mFrom = AutoCleanManager.FROM_UNKNOW;
        }

        //初始化 内存清理设置项
        mLowMemorySummary = findViewById(R.id.low_memory_summary);
        mAutoCleanRAMButton = findViewById(R.id.auto_clean_ram_switch);
        mLowMemoryButton = findViewById(R.id.low_memery_clean_switch);
        mAutoCleanRAMButton.setOnClickListener(this);
        mLowMemoryButton.setOnClickListener(this);

        mRamEnableLayout = findViewById(R.id.ram_enable_layout);
        mLowMemoryLayout = findViewById(R.id.low_memory_layout);
        mRamEnableLayout.setOnClickListener(this);
        mLowMemoryLayout.setOnClickListener(this);
    }

    private void refreshCacheSetting() {
        //mIsUnlock = AutoCleanFeature.AUTO_CACHE.isUnlocked();
        mIsEnable = AutoCleanFeature.AUTO_CACHE.isEnable();

        if (mIsEnable) {
            mEnableSwitch.setBackgroundResource(R.drawable.item_switch_on);
            mFrequencyTitle.setTextColor(getResources().getColor(R.color.autoclean_settings_tips_title));
            mFrequencySummary.setTextColor(getResources().getColor(R.color.autoclean_settings_tips_desc));
            mTotalCleanSummary.setTextColor(getResources().getColor(R.color.autoclean_settings_tips_title));
            mTotalCleanSize.setTextColor(getResources().getColor(R.color.autoclean_settings_total_size));
        } else {
            mEnableSwitch.setBackgroundResource(R.drawable.item_switch_off);
            mFrequencyTitle.setTextColor(getResources().getColor(R.color.autoclean_settings_text_disable));
            mFrequencySummary.setTextColor(getResources().getColor(R.color.autoclean_settings_text_disable));
            mTotalCleanSummary.setTextColor(getResources().getColor(R.color.autoclean_settings_text_disable));
            mTotalCleanSize.setTextColor(getResources().getColor(R.color.autoclean_settings_text_disable));
        }
        int frequency = imContext.getAutoCleanManager().getAutoCleanFrequency();
        switch (frequency) {
            case 6:
                mFrequencySummary.setText(R.string.auto_clean_frequency_6hour);
                break;
            case 12:
                mFrequencySummary.setText(R.string.auto_clean_frequency_12hour);
                break;
            case 24:
                mFrequencySummary.setText(R.string.auto_clean_frequency_1day);
                break;
            case 3 * 24:
                mFrequencySummary.setText(R.string.auto_clean_frequency_3day);
                break;
            case 7 * 24:
                mFrequencySummary.setText(R.string.auto_clean_frequency_7day);
                break;
            default:
        }
    }

    private void refreshMemorySetting() {
        mAutoCleanRAMButton.setBackgroundResource(AutoCleanFeature.AUTO_MEMORY.isEnable() ? R.drawable.item_switch_on : R.drawable.item_switch_off);
        mLowMemoryButton.setBackgroundResource(AutoCleanFeature.LOW_MEMORY.isEnable() ? R.drawable.item_switch_on : R.drawable.item_switch_off);

        if (AutoCleanFeature.AUTO_MEMORY.isEnable()) {
            mLowMemoryButton.setEnabled(true);
            mLowMemorySummary.setTextColor(getResources().getColor(R.color.autoclean_settings_tips_title));
        } else {
            mLowMemoryButton.setEnabled(false);
            mLowMemorySummary.setTextColor(getResources().getColor(R.color.autoclean_settings_text_disable));
        }
    }

    @Override
    protected void doOnResume() {
        refreshViews();
    }

    private void refreshViews() {
        refreshMemorySetting();
        refreshCacheSetting();
    }

    @Override
    public void onClick(View v) {
        doOnClickForCacheSetting(v);
        doOnClickForMemorySetting(v);
    }

    private void doOnClickForMemorySetting(View v) {
        if (v == mAutoCleanRAMButton || v == mRamEnableLayout) {
            if (!AutoCleanFeature.AUTO_MEMORY.isEnable()) {
                GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_SWITCH, CleanGAConstant.L_SMART_CLEAN_MEMORY);
            }

            imContext.getAutoCleanManager().setFeatureEnable(AutoCleanFeature.AUTO_MEMORY, !AutoCleanFeature.AUTO_MEMORY.isEnable());
            if (!AutoCleanFeature.AUTO_MEMORY.isEnable()) {
                imContext.getAutoCleanManager().setFeatureEnable(AutoCleanFeature.LOW_MEMORY, false);
            }
            refreshMemorySetting();

        } else if (v == mLowMemoryButton || v == mLowMemoryLayout) {
            if (!AutoCleanFeature.LOW_MEMORY.isEnable()) {
                GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_SWITCH, CleanGAConstant.L_SMART_CLEAN_LESS);
            }

            imContext.getAutoCleanManager().setFeatureEnable(AutoCleanFeature.LOW_MEMORY, !AutoCleanFeature.LOW_MEMORY.isEnable());
            refreshMemorySetting();
        }
    }

    private void doOnClickForCacheSetting(View v) {
        if (v == mEnableLayout || v == mEnableSwitch) {
            boolean isEnable = !mIsEnable;
            if (isEnable) {
                GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_SWITCH, CleanGAConstant.L_SMART_CLEAN_CACHE);
            }

            imContext.getAutoCleanManager().setFeatureEnable(AutoCleanFeature.AUTO_CACHE, isEnable);
            if (!isEnable) {
                imContext.getAutoCleanManager().stopAutoCleanTask();
            }
            refreshCacheSetting();

        } else if (v == mFrequencyLayout) {
            if (mIsEnable) {
                if (mChooseFrequencyDialog == null) {
                    mChooseFrequencyDialog = new FrequencyChooseDialog(AutoCleanSettingActivity.this);
                    mChooseFrequencyDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            refreshCacheSetting();
                            imContext.getAutoCleanManager().scheduleAutoCleanTrash();
                        }
                    });
                }
                mChooseFrequencyDialog.show();
            } else {
                ToastHelper.create(AutoCleanSettingActivity.this, R.string.adunlock_settings_enable_toast, Toast.LENGTH_SHORT).show();
            }
        } else if (v == mTotalCleanLayout) {
            if (!mIsEnable) {
                ToastHelper.create(AutoCleanSettingActivity.this, R.string.adunlock_settings_enable_toast, Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mChooseFrequencyDialog != null) {
            mChooseFrequencyDialog.dismiss();
        }
    }

}
